﻿create PROCEDURE Sync.SP_LoanInstallmentWarningAttachment_ApplyChanges
@RawValues XML 
AS 
BEGIN
	 
		DECLARE @LoanInstallmentWarningAttachment_Temp TABLE 
		(
			LoanInstallmentWarningAttachmentID UniqueIdentifier,
			[LoanIntallmentWarningID] uniqueidentifier,
				[Attachment] uniqueidentifier,
			IsDeleted BIT, 
			LastUpdatedDate DATETIME
		)
		
	   INSERT INTO @LoanInstallmentWarningAttachment_Temp 
	   SELECT	Tbl.Col.value('LoanInstallmentWarningAttachmentID[1]','uniqueidentifier') LoanInstallmentWarningAttachmentID,
                Tbl.Col.value('LoanIntallmentWarningID[1]', 'uniqueidentifier') [LoanIntallmentWarningID],

Tbl.Col.value('Attachment[1]', 'uniqueidentifier') [Attachment],
				Tbl.Col.value('IsDeleted[1]', 'bit') IsDeleted,
				Tbl.Col.value('LastUpdatedDate[1]','DateTime') LastUpdatedDate
	   FROM   @RawValues.nodes('/DocumentElement/LoanInstallmentWarningAttachment') Tbl(Col)
----------------------------------------------------------------------------------------

-- Insert ------------------------------------------------------------------------------

	INSERT INTO [dbo].[LoanInstallmentWarningAttachment] ([LoanInstallmentWarningAttachmentID],[LoanIntallmentWarningID],[Attachment])
	SELECT t.[LoanInstallmentWarningAttachmentID],t.[LoanIntallmentWarningID],t.[Attachment]
	FROM  @LoanInstallmentWarningAttachment_Temp t
	WHERE t.IsDeleted = 0
    AND NOT EXISTS
    (
        SELECT 1 FROM sync.LoanInstallmentWarningAttachment_Tracking s
        WHERE t.LoanInstallmentWarningAttachmentID = s.LoanInstallmentWarningAttachmentID
    )
	-- put conflicts here.
	
-- Update -------------------------------------------------------------------------------
     
	UPDATE [dbo].[LoanInstallmentWarningAttachment]
	SET [LoanInstallmentWarningAttachment].[LoanIntallmentWarningID] = t.[LoanIntallmentWarningID],[LoanInstallmentWarningAttachment].[Attachment] = t.[Attachment] 
	FROM [dbo].[LoanInstallmentWarningAttachment] s JOIN @LoanInstallmentWarningAttachment_Temp t 
    ON t.LoanInstallmentWarningAttachmentID = s.LoanInstallmentWarningAttachmentID
	WHERE	t.IsDeleted = 0 
	AND		t.LastUpdatedDate IS NOT NULL
	AND		t.LastUpdatedDate >= 
				 isnull((SELECT TOP 1 LastUpdatedDate 
				  FROM sync.LoanInstallmentWarningAttachment_Tracking r
				  WHERE t.LoanInstallmentWarningAttachmentID = r.LoanInstallmentWarningAttachmentID)
                  ,t.LastUpdatedDate)
	-----------------------------------------------------------------------------------------

-- Delete -------------------------------------------------------------------------------

	DELETE [dbo].[LoanInstallmentWarningAttachment]
    FROM @LoanInstallmentWarningAttachment_Temp t JOIN [dbo].[LoanInstallmentWarningAttachment] s
    ON t.LoanInstallmentWarningAttachmentID = s.LoanInstallmentWarningAttachmentID
	WHERE t.IsDeleted = 1

-----------------------------------------------------------------------------------------
	
END











